<?php
class Import extends Controller {

	function Import()
	{
		parent::Controller();
		$this->load->helper('admin');
		
		construct_admin_controller($this->EzAuth_Model);
		
		
	}
	
	
	//do something before we load the page:
	//check to see if you are logged in
	
	function _remap($method) {
		checkauth($this, $method, $this->EzAuth_Model);
	}
	

	
	function index($message = '')
	{
            $data = get_page_ready('import', 0, $this->Settings_Model, $this->Pages_Model, $this->Products_Model, $this->Categories_Model);
		
		$data['location'] = "import";
		
		//from the 'laser' table
		$query = $this->db->get('laser');
		$results = $query->result();
		
		foreach($results as $result) {
			//put the good things into the items table
			$item['title'] = $result->{"Long Desc"};
			$item['description'] = $result->{"MFG."};
			$item['description'] .= "<br />";
			$item['description'] .= $result->{"SYNX P/N"};
			$item['description'] .= "<br />";
			$item['description'] .= $result->{"MFG. P/N"};
			$item['description'] .= "<br />";
			$item['description'] .= $result->{"Long Desc"};
			$item['image'] = '';
			$item['price'] = $result->{"MSRP"};
			$item['sku'] = $result->{"SKU"};
			
			//put it into the database
			$this->db->insert('items', $item);
			
			//put it into the appropriate category
			//if the category exists, put it in there
			$this->db->where('title', $result->{"MFG."});
			$this->db->select('id');
			$query = $this->db->get('categories');
			$res = $query->result();
			if ($query->num_rows() == 1) {
				//the category exists!
				//what is our itemid anyway?
				$this->db->where('sku', $result->{"SKU"});
				$this->db->select('id');
				$q = $this->db->get('items');
				$r = $q->result();
				
				//whew.
				$this->db->insert('item2cat', array('itemid' => $r[0]->id, 'catid' => $res[0]->id));
				
			} else {
				//if i wasn't lazy, i'd have it create a category here. oh well.
			}
		}
		
		$data['message'] = 'Success!';
            $this->load->view('admin/import', $data);
        }
}
?>